package com.sh.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.sh.dao.HandInputDao;
import com.sh.entity.HandInput;
import com.sh.entity.User;
import com.sh.service.HandInputService;
import com.sh.util.DateUtil;

@Service()
public class HandInputServiceImpl implements HandInputService{
	@Autowired
	private HandInputDao handInputDao;

	@Override
	public List<HandInput> listHandInput(Map<String, Object> conMap,User user,String sysTime) {
		// TODO Auto-generated method stub 

		return handInputDao.listHandInput(conMap);

	}
	//上传 excel文件,并且到DB
	@Override
	public String saveUpload(List<HandInput> checkList, User user, String sysTime) {
		// TODO Auto-generated method stub
		handInputDao.saveUpload(checkList,user,sysTime);
		return "";
	}
	//批量 删除 手动输入 信息
	@Override
	public void deleteHandInputs(String[] ids) {
		// TODO Auto-generated method stub
		handInputDao.deleteHandInputs(ids);
	}
	/**
	 * 新增 / 修改 保存数据
	 * */
	@Override
	public void saveHandinputs(HandInput handInput, User user) {
		// TODO Auto-generated method stub
		handInputDao.saveHandinputs(handInput, user);
	}
	//查询数量
	@Override
	public int listHandInputCount(Map<String, Object> conMap, User user, String sysTime) {
		// TODO Auto-generated method stub
		return handInputDao.listHandInputCount(conMap);
	}
	//判断 数据是不是已经存在
	//根据 【日期】【收/付】【客户名】【金额】判断
	@Override
	public boolean isHandInputExist(HandInput handInput) {
		// TODO Auto-generated method stub
		boolean flag = false;
		//修改的时候判断 客户名称有么有存在
		if(!StringUtils.isEmpty(handInput.getId())){
			int count = handInputDao.isHandInputExistForUpdate(handInput);
			flag = (count==0?false:true);
		}else{
			int count = handInputDao.isHandInputExistForAdd(handInput);
			flag = (count==0?false:true);
		}

		return flag;

	}
	//根据ID 查询对应日期
	@Override
	public List<String> listYearByIds(String[] ids) {
		// TODO Auto-generated method stub
		List<String> rtnList = new ArrayList<>();
		List list = handInputDao.listYearByIds(ids);
		if(list!=null &&
				list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map = (Map)list.get(i);
				String date =
						(String)map.get("rec_pay_date");
				if(DateUtil.checkDateFormat(date, "yyyy-MM-dd")){
					if(!rtnList.contains(date.split("-")[0])){
						rtnList.add(date.split("-")[0]);
					}
				}
			}
			return rtnList;

		}
		return null;
	}

}
